package com.example.gamecenter.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.Map;

//注意WebView要继承com.tencent.smtt.sdk.WebView包下的
//并非android.webkit.WebView包
public class X5WebView extends WebView {
	//设置顶部加载进度条
    private ProgressBar mprogressBar;
	//给自定义类创建构造方法，一般Alt+insert -> constructor -> 全部构造
	// initWebView(context)为WebView作配置
    public X5WebView(Context context) {
        super(context);
        initWebView(context);
    }

    public X5WebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        initWebView(context);
    }

    public X5WebView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        initWebView(context);
    }

    public X5WebView(Context context, AttributeSet attributeSet, int i, boolean b) {
        super(context, attributeSet, i, b);
        initWebView(context);
    }

    public X5WebView(Context context, AttributeSet attributeSet, int i, Map<String, Object> map, boolean b) {
        super(context, attributeSet, i, map, b);
        initWebView(context);
    }

	// initWebView(context)为WebView作配置
    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView(Context context) {
        //添加进度条
        mprogressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
        //LayoutParams实现xml布局动态设置，主要设置View的位置(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 10);
        mprogressBar.setLayoutParams(layoutParams);
        mprogressBar.setProgress(0);
        addView(mprogressBar);

        //设置Settings，可直接照搬
        WebSettings settings = this.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setUseWideViewPort(true);
        settings.setSupportMultipleWindows(true);
        settings.setAppCacheEnabled(true);
        //此项影响网页的加载功能
        settings.setDomStorageEnabled(true);
        settings.setGeolocationEnabled(true);
        settings.setAppCacheMaxSize(Long.MAX_VALUE);
        settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        //初始化WebViewClient
        setWebViewClient(new WebViewClient(){
            //页面内跳转，禁止调用浏览器打开新链接
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return super.shouldOverrideUrlLoading(webView, s);
            }
        });

        //初始化WebChromeClient
        setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int i) {
                super.onProgressChanged(webView, i);
                if (i == 100) {
                    mprogressBar.setVisibility(GONE);
                } else {
                    if (mprogressBar.getVisibility() == GONE){
                        mprogressBar.setVisibility(VISIBLE);
                    }
                    mprogressBar.setProgress(i);
                }
            }
        });
    }
}

